var location = require("../../../mybehaviors/location/location");
const app = getApp();
const pageOptions = {
  // 页面数据
  behaviors: [location],
  data: {
    isFirstOnShow: true, // 是否为首次执行onShow
    checked_agreement: false,
    inpval: "",
    lng: "",
    lat: "",
    shop_agreement_title: "邀请码协议",
    shop_agreement_text: "",
  },
  // 页面载入时
  onLoad(e) {
    this.init(e);
  },
  // 页面初始化
  init(e) {
    console.log(e)
    this.data.options = e;
    if (this.data.options.scene) {
      let scene = decodeURIComponent(this.data.options.scene);
      if (scene) {
        var info_arr = [];
        info_arr = scene.split('&');
        for (let i = 0; i < info_arr.length; i++) {
          let chil_arr = [];
          chil_arr = info_arr[i].split('=');
          console.log(chil_arr)
          if (chil_arr[0] == 'code') {
            this.setData({
              inpval: chil_arr[1]
            })
          }
          if (chil_arr[0] == 'mid') {
            app._setMid(chil_arr[1]);
          }
        }
      }
    }
    console.log(this.data.options.code)
    if (this.data.options.code) {
      this.setData({
        inpval: this.options.code,
      });
    }
    this._getLocation((res) => {
      this.setData({
        lng: res.location.lng,
        lat: res.location.lat,
      });
      console.log(this.data.lng, this.data.lat, res, 'dddddddddddwwwwwwwwwwww')
      this.getData();
    }, (e) => {
      wx.showModal({
        title: '定位失败',
        content: '请确认授权定位是否开启',
        confirmText: '手动定位',
        complete: (res) => {
          if (res.confirm) {
            wx.navigateTo({
              url: '/packageC/o2o/o2oLocation_loc/o2oLocation_loc?tag=cloud_shop',
            })
          }
        }
      })
    });
  },
  onChange(event) {
    console.log()
    this.setData({
      checked_agreement: event.detail,
    });
  },
  // 页面准备好时
  onReady() {},
  // 页面显示时
  onShow() {
    const {
      isFirstOnShow
    } = this.data;

    if (isFirstOnShow) {
      // 首次执行时
      this.setData({
        isFirstOnShow: false,
      });
      return;
    }
  },
  // 页面隐藏时
  onHide() {},
  // 页面卸载时
  onUnload() {},
  // 下拉页面时
  onPullDownRefresh() {},
  // 到达页面底部时
  onReachBottom() {},
  // 页面滚动时
  onPageScroll() {},
  // 分享时，注：onShareAppMessage不能为async异步函数，会导致不能及时取得返回值，使得分享设置无效
  onShareAppMessage() {
    /* const title = ''
    const path = ''
    const imageUrl = ``

    return {
      title,
      path,
      imageUrl,
    } */
  },
  closeTap() {
    this.setData({
      checked_agreement: false,
    });
    wx.redirectTo({
      url: "/packageG/index/index",
    });
  },
  async becomeStoreTap() {
    if (!this.data.checked_agreement && this.data.shop_agreement_text) {
      // this.$toast("请先阅读并同意协议");
      app.tips("请先阅读并同意协议");
      return;
    }

    let json = {
      code: this.data.inpval,
      longitude: this.data.lng,
      latitude: this.data.lat,
    };
    let {
      data
    } = await app._postNetWork({
      url: app.getNetAddresss("plugin.cloud-shop.frontend.code.becomeStore"),
      data: json,
    });
    if (data.result) {
      app.tips(data.msg);
      wx.redirectTo({
        url: "/packageG/index/index",
      });
      this.initPopupClose();
    } else {
      app.tips(data.msg);
    }
  },
  toAg() {
    wx.navigateTo({
      url: "/mircoApp/cloud_shop/cloud_shop_agreement/cloud_shop_agreement",
    });
  },
  initPopupClose() {
    this.setData({
      checked_agreement: false,
    });
  },
  async getData() {
    let {
      data
    } = await app._postNetWork({
      url: app.getNetAddresss("plugin.cloud-shop.frontend.code.agreement"),
    });
    if (data.result) {
      this.setData({
        shop_agreement_text: data.data.shop_agreement_text,
        shop_agreement_title: data.data.shop_agreement_title,
      });
    } else {
      app.tips(data.msg);
    }
  },
};

Page(pageOptions);